#!/usr/bin/python
import unittest
import sys
import os
from StringIO import StringIO

from BeautifulSoup import BeautifulSoup
import cherrypy
from cherrypy.test import helper

tests_dir = os.path.dirname(sys.argv[0])

from uya.configmod_web import configuration

callbacks = {}
#authlst = [("user", "password"), ("user2", "password2")]
authlst = None

configcontents = """
    [section1]  
        parameter1 = value1
        parameter2 = 10
        parameter3 = yes
        parameter4 = 192.168.1.1, 10.0.0.1
        parameter5 = option2  
        [[section1a]]
            x = 1a
        [[section1b]]
            y = 1b
"""

templatecontents = """
    [section1] 
        _parameter1 = meta(help="help text", default=None)
        parameter1 = string
        parameter2 = integer        
        parameter3 = boolean
        _parameter4 = meta(fields="4|(\d)+.(\d)+.(\d)+.(\d)+|%s.%s.%s.%s", default=None)
        parameter4 = ip_addr_list
        parameter5 = option('option1', 'option2', default='option2', default=None)
        [[section1a]]
            x = string
        [[section1b]]
            y = string
"""

def setup_server(title):
    html_templates_dir = os.path.join(tests_dir, "../templates")
    handler = configuration.Configuration(title=title, 
        configuration_file=StringIO(configcontents), 
        template_file=StringIO(templatecontents), 
        html_templates_dir=html_templates_dir, 
        callbacks={}, authlst=authlst)
    cherrypy.config.update({
            'server.log_to_screen': False,
            'server.socket_port': 8081,
            'autoreload.on': False,
            'log_debug_info_filter.on': False,
    })    
    cherrypy.tree.mount(handler, "/")


def get_soup(html):
    return BeautifulSoup(html)

class TestMiscLibrary(helper.CPWebCase):        
#    interactive = False
        
    def test_configuration(self):
        self.getPage("/admin/section1")        
        self.assertStatus('200 OK')
        self.assertInBody("<!DOCTYPE")
        soup = get_soup(self.body)
        ul_tags = soup.findAll("ul")
        self.assertEqual(2, len(ul_tags))
        self.assertEqual(1, len(ul_tags[0].findAll("li")))
        self.assertEqual(2, len(ul_tags[1].findAll("li")))
        form = soup.find("form")
        self.assertTrue(form)
        group = form.find("div", {"id": "group-"})
        self.assertTrue(form)
        parameters_divs = group.findAll("div", recursive=False)
        self.assertEqual(5, len(parameters_divs))
        self.assertTrue(parameters_divs[0].find("input", 
            dict(type="text", name="parameter1", value="value1")))
        self.assertTrue(parameters_divs[0].find("label", 
            {"title": "help text",}))
        self.assertTrue(parameters_divs[1].find("input", 
            dict(type="text", name="parameter2", value="10")))
        self.assertTrue(parameters_divs[2].find("input", 
            dict(type="checkbox", name="parameter3", checked="checked")))
        #from ipdb import set_trace; set_trace()
        select = parameters_divs[4].find("select")
        self.assertTrue(select)
        options = select.findAll("option")
        self.assertEqual(2, len(options))
        self.assertTrue({u"value": u"option1"}, options[0].attrs)
        self.assertTrue({u"value": u"option2", u"selected": u"selected"}, 
            options[1].attrs)
                                             
if __name__ == '__main__':
    setup_server("mytitle")
    helper.testmain()
